package com.zzptc.easyshop.servlet;

import com.zzptc.easyshop.dao.GoodTypeDaoImp;
import com.zzptc.easyshop.dao.UserDao;
import com.zzptc.easyshop.vo.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "Login_userServlet")
public class Login_userServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Date ss=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=simpleDateFormat.format(ss.getTime());
        PrintWriter pw=response.getWriter();
//        pw.print(123);

        String uname=request.getParameter("userName");
        String pwd=request.getParameter("passWord");
        int role=Integer.parseInt(request.getParameter("type"));
        GoodTypeDaoImp.IDAO dao=new UserDao();
        Users users=new Users();
        users.setEu_user_id(uname);
        users.setEu_password(pwd);
        users.setEu_status(role);
        boolean flag=false;
        try {
            flag=dao.login(users);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        HttpSession session=request.getSession();

        if (flag){
            session.setAttribute("role",role);
            session.setAttribute("username",uname);
            session.setAttribute("time",time);
            session.setAttribute("loginerror","");
            if (role==1)
                request.getRequestDispatcher("productServlet").forward(request,response);
            if (role==2)
                request.getRequestDispatcher("usermanagerservlet").forward(request,response);
        }else {
            session.setAttribute("loginerror","登陆失败");
            request.getRequestDispatcher("productServlet").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
